package com.game.model.task;

import com.game.data.BagData;
import com.game.model.goods.Goods;
import com.game.model.role.Role;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

/**
 * @Description:任务
 **/
public abstract class Task implements Serializable {
    private int id;//任务编号
    private String name;//任务名称
    private String decription;//任务描述
    private Map<Role,int[]> fightTask;//击杀任务，击杀对象，次数如（小黄蜂，0/20只） 数组0.自己完成的，数组1.需要完成的数量
    private Map<Goods,Integer> goodsTask;//提交任务道具，物品，个数
    private Map<Goods,Integer> rewards;//任务奖励


    //提交任务
    public boolean submitTask(){
        if (fightTask!=null){
            Iterator<Map.Entry<Role, int[]>> iterator = fightTask.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<Role, int[]> next = iterator.next();
                int[] value = next.getValue();
                //如果数量不够
                if (value[0]<value[1])
                    return false;
            }
        }
        if (goodsTask!=null){
            Iterator<Map.Entry<Goods, Integer>> iterator = goodsTask.entrySet().iterator();
            Iterator<Map.Entry<Goods, Integer>> iterator1 = BagData.bag.getGoodsMap().entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<Goods, Integer> next = iterator.next();
                while (iterator1.hasNext()){
                    Map.Entry<Goods, Integer> next1 = iterator1.next();
                    //如果背包里有任务要的物品，比较大小
                    if (next.getKey().equals(next1.getKey())){
                        //如果拥有的小于需要的
                        if (next1.getValue()<next.getValue()){
                          return false;
                        }
                    }
                }
            }
            //从背包里移除任务物品
           return  BagData.bag.removeToBag(goodsTask);
        }
        return true;
    }


    public Task() {
    }

    public Task(int id, String name, String decription, Map<Role, int[]> fightTask, Map<Goods, Integer> goodsTask, Map<Goods, Integer> rewards) {
        this.id = id;
        this.name = name;
        this.decription = decription;
        this.fightTask = fightTask;
        this.goodsTask = goodsTask;
        this.rewards = rewards;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Task task = (Task) o;
        return id == task.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDecription() {
        return decription;
    }

    public void setDecription(String decription) {
        this.decription = decription;
    }

    public Map<Role, int[]> getFightTask() {
        return fightTask;
    }

    public void setFightTask(Map<Role, int[]> fightTask) {
        this.fightTask = fightTask;
    }

    public Map<Goods, Integer> getGoodsTask() {
        return goodsTask;
    }

    public void setGoodsTask(Map<Goods, Integer> goodsTask) {
        this.goodsTask = goodsTask;
    }

    public Map<Goods, Integer> getRewards() {
        return rewards;
    }

    public void setRewards(Map<Goods, Integer> rewards) {
        this.rewards = rewards;
    }

}
